package com.training.java.redbox.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.training.java.redbox.domain.DAOException;
import com.training.java.redbox.domain.Movie;
import com.training.java.redbox.domain.MovieDAO;
import com.training.java.redbox.domain.MovieDAOJDBCImpl;

/**
 * Servlet implementation class DeleteServlet
 */
public class DeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		long id = Long.parseLong(request.getParameter("id"));
		HttpSession session = request.getSession();

		
		List<Movie> movies = (List<Movie>)session.getAttribute("MoviesCart");
		List<Long> cart = (List<Long>) session.getAttribute("cart");
		Movie movie = null;
		MovieDAO dao = new MovieDAOJDBCImpl();
		try {
				movie = dao.findByPrimaryKey(id);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		movies.remove(movie);
		cart.remove(id);
		session.setAttribute("cart", cart);
		session.setAttribute("MoviesCart", movies);
		RequestDispatcher rd = request
				.getRequestDispatcher("/checkout");
		rd.forward(request, response);	
		
	}

}
